package esClient;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2020/2/15.
 */
public class HighLevelClient {
    private RestHighLevelClient client;
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Before
    public void init(){
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("192.168.32.128", 9200, "http"));
        client = new RestHighLevelClient(restClientBuilder);
    }
    @After
    public void close() throws Exception{
        client.close();
    }
    //同步插入
    @Test
    public void testCreate()throws Exception{
        Map<String,Object> data = new HashMap<>();
        data.put("id","1003");
        data.put("name","希望");
        data.put("age", "3");
        data.put("sex", "女");
        IndexRequest indexRequest = new IndexRequest("es","user").source(data);
        IndexResponse indexResponse = this.client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("id->"+indexResponse.getId());
        System.out.println("index->"+indexResponse.getIndex());
        System.out.println("type->"+indexResponse.getType());
        System.out.println("version->"+indexResponse.getVersion());
        System.out.println("ShardInfo->"+indexResponse.getShardInfo());
    }
    //异步插入
    @Test
    public void testCreateAsync()throws Exception{
        Map<String,Object> data = new HashMap<>();
        data.put("id", "1004");
        data.put("name", "眷念");
        data.put("age", "4");
        data.put("sex", "男");
        IndexRequest indexRequest = new IndexRequest("es","user").source(data);
        this.client.indexAsync(indexRequest, RequestOptions.DEFAULT, new ActionListener<IndexResponse>() {
            @Override
            public void onResponse(IndexResponse indexResponse) {
                System.out.println("id->" + indexResponse.getId());
                System.out.println("index->" + indexResponse.getIndex());
                System.out.println("type->" + indexResponse.getType());
                System.out.println("version->" + indexResponse.getVersion());
                System.out.println("ShardInfo->" + indexResponse.getShardInfo());
            }

            @Override
            public void onFailure(Exception e) {
                System.out.println("失败原因" + e);
            }
        });
        System.out.println("ok");
        Thread.sleep(20000);
    }

    @Test
    public void testSearch()throws Exception{
        SearchRequest searchRequest = new SearchRequest("es");
        searchRequest.types("user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchQuery("name","希望"));
        sourceBuilder.from(0);
        sourceBuilder.size(5);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = this.client.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println("搜索到 "+searchResponse.getHits().getTotalHits()+"数据");
        SearchHits hits = searchResponse.getHits();
        for(SearchHit hit: hits){
            String json = hit.getSourceAsString();
            System.out.println(json);
            User user = MAPPER.readValue(json, User.class);
            System.out.println(user);
        }
    }
}
